package cn.dy.sbo.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import cn.common.lib.springside.web.CrudActionSupport;
import cn.common.lib.util.web.ParamUtils;
import cn.dy.sbo.core.UserManager;
import cn.dy.sbo.model.User;

public class UserregAction extends CrudActionSupport<User>
{
    private static final long serialVersionUID = 1L;

    private UserManager       userManager;

    @Autowired
    public void setUserManager(UserManager userManager)
    {
        this.userManager = userManager;
    }

    private User user; // 定义用户

    public void setUser(User user)
    {
        this.user = user;
    }

    @Override
    public String save() throws Exception
    {
        try
        {

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "succ";
    }

    public String checkName()
    {

        HttpServletRequest request = ServletActionContext.getRequest();
        String newName = ParamUtils.getParameter(request, "name");
        String oldName = ParamUtils.getParameter(request, "oldName");
        if (userManager.isExistByName(newName, oldName))
        {
            Struts2Utils.renderText("false");
        }
        else
        {
            Struts2Utils.renderText("true");
        }
        // 因为直接输出内容而不经过jsp,因此返回null.
        return null;
    }

    @Override
    public String input() throws Exception
    {
        return SUCCESS;
    }

    @Override
    public String list() throws Exception
    {
        return SUCCESS;
    }

    @Override
    protected void prepareModel() throws Exception
    {
        user = new User();
    }

    @Override
    public User getModel()
    {
        return user;
    }
}
